package com.wen.srb.common.util;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author：Win
 * @Date: 2021/7/12 22:42
 * @Description: 统一返回结果类
 */
@Data
public class R {
    private Integer code;
    private String msg;
    private Map<String, Object> data;

    private R() {
        data = new HashMap<>(16);
    }

    public static R error() {
        R r = new R();
        r.setCode(ResponseEnum.ERROR.getCode());
        r.setMsg(ResponseEnum.ERROR.getMsg());
        return r;
    }

    public static R error(String msg) {
        R r = new R();
        r.setCode(ResponseEnum.ERROR.getCode());
        r.setMsg(msg);
        return r;
    }

    public static R ok() {
        R r = new R();
        r.setCode(ResponseEnum.SUCCESS.getCode());
        r.setMsg(ResponseEnum.SUCCESS.getMsg());
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.setCode(ResponseEnum.SUCCESS.getCode());
        r.setMsg(msg);
        return r;
    }

    public R code(Integer code) {
        this.setCode(code);
        return this;
    }

    public R msg(String msg) {
        this.setMsg(msg);
        return this;
    }

    public R data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public R data(Map<String, Object> map) {
        this.data.putAll(map);
        return this;
    }
}
